<?php

namespace app\common\logic;


use app\common\traits\MessagePart;

abstract class BaseLogic
{

    use MessagePart;

    private function __clone()
    {
        ## 禁止clone
    }


    /**
     * logic容器
     * @var array<string,object>
     */
    private static array $container = [];

    /**
     * @param ...$args
     * @return object|static|null
     */
    public static function instance(...$args)
    {
        //解析调用的类
        $classPath = static::class;
        $instance = self::$container[$classPath] ?? null;
        if (!$instance) {
            //new该类
            $instance = self::$container[$classPath] = new static(...$args);
        }
        return $instance;
    }

    /**
     * 不缓存到容器里
     * @param mixed ...$args
     * @return static
     */
    public static function nonContainerInstance(...$args)
    {
        return new static(...$args);
    }

    public static function __callStatic($name, $arguments)
    {
        $instance = self::instance();
        if (!method_exists($instance, $name)) {
            throw new \RuntimeException(sprintf("method[%s] not found in php file[%s]", $name, static::class));
        }
        return $instance->{$name}(...$arguments);
    }
}
